package com.geospatial.algorithm.util;

import com.geospatial.algorithm.entity.s2geometry.Point;

/**
 * @ClassName: S2GeometryUtil
 * @Description: s2geometry工具类
 * @Author
 * @Date 2021/7/20
 * @Version 1.0
 */
public class S2GeometryUtil {
    private static final double DEG_TO_RAD = Math.PI / 180; //2Math.PI/360,单位弧度长
    private static final double RAD_TO_DEG = 180 / Math.PI;

    public static Point LatLng(double lon, double lat, Boolean noWrap) {
        Point point = new Point();
        if (noWrap) {
            lat = Math.max(Math.min(lat, 90), -90);                 // clamp latitude into -90..90
            lon = (lon + 180) % 360 + ((lon < -180 || lon == 180) ? 180 : -180);   // wrap longtitude into -180..180
        }
        point.setLat(lat);
        point.setLon(lon);
        return point;
    }

    public static int largestAbsComponent(double[] xyz) {
        double[] temp = new double[]{Math.abs(xyz[0]), Math.abs(xyz[1]), Math.abs(xyz[2])};
        if (temp[0] > temp[1]) {
            if (temp[0] > temp[2]) {
                return 0;
            } else {
                return 2;
            }
        } else {
            if (temp[1] > temp[2]) {
                return 1;
            } else {
                return 2;
            }
        }
    }

    public static double[] largestAbsComponent(int face,double[] xyz) {
        double[] uv = new double[2];
        double u,v;
        switch (face) {
            case 0: u =  xyz[1]/xyz[0]; v =  xyz[2]/xyz[0]; break;
            case 1: u = -xyz[0]/xyz[1]; v =  xyz[2]/xyz[1]; break;
            case 2: u = -xyz[0]/xyz[2]; v = -xyz[1]/xyz[2]; break;
            case 3: u =  xyz[2]/xyz[0]; v =  xyz[1]/xyz[0]; break;
            case 4: u =  xyz[2]/xyz[1]; v = -xyz[0]/xyz[1]; break;
            case 5: u = -xyz[1]/xyz[2]; v = -xyz[0]/xyz[2]; break;
            default:
                throw new IllegalStateException("Invalid face: " + face);
        }
        uv[0] = u;
        uv[1] = v;
        return uv;
    }
}
